// node请求
const express = require('express')
const app = express()
// 解决跨域
const cors = require('cors')
app.use(cors())
// post接收请求参数
const bodyParser = require('body-parser')
// 解决JSON
app.use(bodyParser.json())
// 解析URL编码数据 qs库来解析
// app.use(express.urlencoded({ extended: true }))
// 转JSON
const myMixin = require('./utils/JsonToTxt')
// 写入依赖
const fs = require('fs')
// 需要的参数
const { dataForm } = require('./JSON/WaterElectricity')
// const { xxx } = require('./JSON/WaterElectricity')
// 封装写入方法
function writeTxt (path, writePath, postData, time) {
  setInterval(async () => {
    // 请求水电表
    const data = await myMixin(path, postData)
    // 写入
    fs.writeFileSync(writePath, JSON.stringify(data))
  }, time)
}

// 请求接口路径 写入路径 请求时间
writeTxt('/device/currentValue', './Txt/WaterElectricity.txt', dataForm, 5000)

app.listen(3001, () => {
  console.log(`Server running at http://localhost:${3001}`)
})
